package com.example.myapplication.tab;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.myapplication.R;
import com.example.myapplication.tab.adapter.BasePagerAdapter;
import com.example.myapplication.tab.fragments.OldRentalFragment;
import com.example.myapplication.tab.fragments.SortRentalFragment;

import java.util.ArrayList;
import java.util.List;

public class TabActivity extends FragmentActivity {

    private ViewPager viewPager;
    private LinearLayout linearLayout;
    private List<Fragment> fragmentList;
    private List<String> titleList;
    private BasePagerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab);

        initView();
        initData();
    }

    private void initData() {
        initTabs();
        adapter = new BasePagerAdapter(getSupportFragmentManager(), fragmentList, titleList);
        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(15);
    }

    private void initTabs() {
        fragmentList = new ArrayList<>();
        titleList = new ArrayList<>();

        fragmentList.add(new SortRentalFragment());
        fragmentList.add(new OldRentalFragment());

        titleList.add("新车");
        titleList.add("准新车");
    }

    private void initView() {
        TabLayout tab_layout = findViewById(R.id.tab_layout_news);
        viewPager = findViewById(R.id.view_pager_news);

        tab_layout.setupWithViewPager(viewPager);
        tab_layout.setTabMode(TabLayout.MODE_SCROLLABLE);
        ImageView add_channel_iv = findViewById(R.id.add_channel_iv);
        linearLayout = findViewById(R.id.header_layout);
        linearLayout.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
    }
}
